package com.ytjj.qmyx.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.request.*;
import com.ytjj.qmyx.admin.model.response.ReissueLogisticsResponse;
import com.ytjj.qmyx.admin.service.ReissueLogisticsService;
import com.ytjj.qmyx.admin.utils.UserUtil;
import com.ytjj.qmyx.mall.model.ReissueLogistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 补单管理-销售渠道
 */
@RestController
@RequestMapping("/reissueLogistics")
public class ReissueLogisticsController {

    @Autowired
    private ReissueLogisticsService reissueLogisticsService;
    @Autowired
    private SupplyApiConfig supplyApiConfig;

    /**
     * 补单物流列表
     * @param logisticsRequest
     * @return
     */
    @PostMapping("/list")
    @OperLog(operModule = "补单物流模块", operDesc = "补单物流列表", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult<CommonPage<ReissueLogisticsResponse>> selectLogistics(@RequestBody LogisticsRequest logisticsRequest) {
        return CommonResult.success(reissueLogisticsService.getReissueLogisticsList(logisticsRequest));
    }

    @PostMapping("/exportList")
    public CommonResult exportList(@RequestBody LogisticsRequest logisticsRequest) {
        return CommonResult.success(reissueLogisticsService.exportList(logisticsRequest));
    }


    @GetMapping("/updatefinishStatus")
    @OperLog(operModule = "补单物流模块", operDesc = "修改完结状态", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updatefinishStatus(Integer id) {
            return CommonResult.success(reissueLogisticsService.updatefinishStatus(id,new Date()));
    }

    @GetMapping("/updateSupplyChainFinishStatus")
    public CommonResult updateSupplyChainFinishStatus(@RequestParam(value = "id") Integer id ,
                                                      @RequestParam(value = "childOrderNo")String childOrderNo) {
        return CommonResult.success(reissueLogisticsService.updateSupplyChainFinishStatus(id,childOrderNo));
    }

    /**
     * 修改供应链补单理由
     * @param id
     * @param logisticsReason
     * @return
     */
    @GetMapping("/updateSupplyChainLogisticsReason")
    public CommonResult updateSupplyChainLogisticsReason(@RequestParam(value = "id") Integer id ,
                                                         @RequestParam(value = "logisticsReason")String logisticsReason,
                                                         @RequestParam(value = "childOrderNo")String childOrderNo) {
        reissueLogisticsService.updateSupplyChainLogisticsReason(id,logisticsReason,childOrderNo);
        return CommonResult.success();
    }



    /**
     * @Description 批量操作
     * @Version 1.0
     **/
    @PostMapping("/batchHandle")
    public CommonResult batchHandle(@RequestBody IdsRequest idsRequest) {
        reissueLogisticsService.batchHandle(idsRequest.getIds());
        return CommonResult.success(idsRequest.getIds());
    }

    @PostMapping("/updateRetrieveInfo")
    @OperLog(operModule = "补单物流模块", operDesc = "修改补单数据", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult updateRetrieveInfo(@RequestBody ReissueLogisticsRetrieveRequest request) {
        return reissueLogisticsService.updateRetrieveInfo(request,UserUtil.getAdminId());
    }

    /**
     *增加补单物流
     * @param reissueLogistics
     * @return
     */
    @PostMapping("/insertLogistics")
    @OperLog(operModule = "补单物流模块", operDesc = "增加补单物流", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult insertLogistics(@Validated @RequestBody ReissueLogistics reissueLogistics) {
        return reissueLogisticsService.insertLogistics(reissueLogistics);
    }

    @GetMapping("/updateStatus")
    @OperLog(operModule = "补单物流模块", operDesc = "修改补单物流状态", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateStatus(@RequestParam Integer id) {
        int count = reissueLogisticsService.updateStatus(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 获取补单物流（从供应链获取）
     */
    @PostMapping("/expressInfo")
    public CommonResult getExpressInfo(@RequestBody ReissueLogistics reissueLogistics) {
        if (reissueLogistics.getExpressCompanyNo() == null ||
                reissueLogistics.getExpressNo() == null || reissueLogistics.getUserPhone()== null) {
            return CommonResult.success(null, "暂无物流信息!");
        } else {
            Map<String,String> request = new HashMap<>();
            request.put("expressCode",reissueLogistics.getExpressCompanyNo());
            request.put("expressNo",reissueLogistics.getExpressNo());
            request.put("tel",reissueLogistics.getUserPhone());
            String result = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/express/getReplacementOrderInfo",request, supplyApiConfig.getHeadsMap());
            CommonResult commonResult = JSONObject.parseObject(result, CommonResult.class);
            return commonResult;
        }
    }

    /**
     * 删除补单记录
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody LogisticsDeleteRequest request) {
        Integer adminId = UserUtil.getAdminId();
        request.setAdminId(adminId);
        int count = reissueLogisticsService.delReissueLogistics(request);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/exportData")
    public CommonResult exportData(@RequestBody LogisticsRequest logisticsRequest) {
        return CommonResult.success(reissueLogisticsService.exportData(logisticsRequest));
    }

    /**
     * 是否完结修改
     */
    @PostMapping("/finishedReissueLogistics")
    public CommonResult finishedReissueLogistics(@RequestBody LogisticsFinishRequest logisticsFinishRequest) {
        reissueLogisticsService.finishedReissueLogistics(logisticsFinishRequest);
        return CommonResult.success();
    }

}
